package com.cbgolf.oa.activity.order;

import android.os.Bundle;
import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.contract.IOrderDetailsContract.IOrderDetailsPresenter;
import com.cbgolf.oa.contract.IOrderDetailsContract.IOrderDetailsView;
import com.cbgolf.oa.contract.IOrderDetailsContract.IOrderDetailsWaiter;
import com.cbgolf.oa.event.EventOrder;
import com.cbgolf.oa.presenter.OrderDetailsPresenterImp;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DialogUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.views.OrderDetailsViewImp;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author lq
 * @date 2018/4/12
 */

public class OrderDetailsActivity extends BaseActivity implements IOrderDetailsWaiter {

  /**
   * 是否离开过 回来刷新
   */
  private boolean isLeave = false;
  /**
   * order id
   */
  private String orderId;
  /**
   * 订单类型 如：teetime 酒店
   */
  private int orderType;

  private IOrderDetailsPresenter presenter;
  private IOrderDetailsView view;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_order_details);
  }

  @Override
  protected void findViews() {
    EventBus.getDefault().register(this);
    orderId = DataUtil.getStringExtra(getIntent(), "orderId");
    orderType = DataUtil.getIntExtra(getIntent(), "type", 0);
    view = new OrderDetailsViewImp(this, this, orderType);
    presenter = new OrderDetailsPresenterImp(view);
  }

  @Override
  protected void loadData() {
    presenter.requestData(orderId, EnumUtil.isMatch(orderType));
  }

  @Subscribe(threadMode = ThreadMode.MAIN)
  public void requestOver(EventOrder event) {
    view.requestOver(event);
  }

  @Override
  public String getActivityTitle() {
    return "订单详情";
  }

  @Override
  public void back() {
    finish();
  }

  @Override
  protected void onResume() {
    if (isLeave && !Util.isNull(orderId)) {
      presenter.requestData(orderId, EnumUtil.isMatch(orderType));
    }
    super.onResume();
  }

  @Override
  protected void onPause() {
    isLeave = true;
    super.onPause();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
  }


  @Override
  public void doSubmit() {
    DialogUtil.showOrderConfirmDialog(context,
        "温馨提示",
        "亲，您确定要确认该订单吗？",
        () -> presenter.doSubmit(EnumUtil.isHotel(orderType), orderId));
  }

  @Override
  public void doCancel() {
    DialogUtil.showOrderConfirmDialog(context,
        "温馨提示",
        new StringBuilder()
            .append("亲，您确定要")
            .append(EnumUtil.isHotel(orderType) ? "取消" : "拒绝")
            .append("该订单吗？").toString(),
        () -> presenter.doCancel(EnumUtil.isHotel(orderType), orderId));
  }
}

